<?php namespace App\Models\Base;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis; 
use Illuminate\Support\Facades\DB;
 
class CxhshopBase extends Model {

    protected $connection='jmft_cxh';
	//生成订单号 充值号等等 
    public function createNO($table, $field, $prefix)
	{
		$billno = date('YmdHis') . $this->random(6, true);
		$table = 'ims_ewei_shop_'.$table;
		
		while (1) {
            $count =  DB::connection('jmft_cxh')->table($table)->where($field,'=',$billno)->count();
			// $count = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_' . $table) . ' where ' . $field . '=:billno limit 1', array(':billno' => $billno));
			if ($count <= 0) {
				break;
			}
			$billno = date('YmdHis') .  $this->random(6, true);
		}
		return $prefix . $billno;
	}
	//生成随机数目
	function random($length, $numeric = FALSE) {
		$seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
		$seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
		if ($numeric) {
			$hash = '';
		} else {
			$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
			$length--;
		}
		$max = strlen($seed) - 1;
		for ($i = 0; $i < $length; $i++) {
			$hash .= $seed{mt_rand(0, $max)};
		}
		return $hash;
	}

	/**
     * 财务中心 根据总数据得到时间数据
     * params1 array $list
     */
	public function getTimeData($list) {
		foreach($list as $key => $value) {

			$time[] = date("Y-m",$value['ctime']);
			$list[$key]['ym'] = date("Y-m",$value['ctime']);
			$list[$key]['ctime'] = date("Y-m-d H:i:s",$value['ctime']);

		}
        return $list;

	}

	/**
     * 封装关联模型查询 只去部分字段
     * params1 $query 
     * params2 string $relation 对应关联模型
     * params3 array $columns  需要取得字段（必须包含关联字段）
     */
	public function scopeWithCertain($query, $relation, $columns) {
		return $query -> with([$relation => function($query) use ($columns) {
			$query -> select($columns);
		}]);
	}
	/**
     * 暂时在这里获取提成比列
     * 
     */
	public function getParameter(){  
		$arr =array();
		$arr['direct_reward'] = 0.2;
		$arr['Inter_reward'] = 0.1;
		$arr['all_province_gradation'] = 0.05;
		$arr['all_city_gradation'] = 0.05;
		$arr['all_zone_gradation'] = 0.1;
		$arr['two_province_gradation'] = 0.05;
		$arr['two_city_gradation'] = 0.15;
		$arr['single_province_gradation'] = 0.2;
		$arr['gift_order'] = 0.03;
		$arr['good_zhitui'] = 0.05;
		$arr['good_jiantui'] = 0.02;
		$arr['goods_province'] = 0.005;
		$arr['goods_city'] = 0.01;
		$arr['goods_zone'] = 0.015;
		$arr['car_jiantui'] = 0.02;
		$arr['car_province'] = 0.005;
		$arr['car_city'] = 0.01;
		$arr['car_zone'] = 0.015;
		$arr['maintenance_fee'] = 0.05;
		$arr['shopping_points'] = 0.1;
		$arr['bill_charge'] = 0.05;
		$arr['bonus'] = 0.02;
		$arr['agent_money_ratio'] =1;
		$arr['agent_point_ratio'] =0;
		$arr['agent_platform_ratio'] =0;
		$arr['car_taxation'] =0;
		$arr['car_ratio'] =0.0006;
		$arr['car_money_ratio'] =0.8;
		$arr['car_point_ratio'] =0.2;
		$arr['car_platform_ratio'] =0;
		$arr['specialgoods_ratio'] =0.0005;
		$arr['specialgoods_money_ratio'] =0;
		$arr['specialgoods_point_ratio'] =0;
		$arr['specialgoods_platform_ratio'] =0;
		$arr['month_bonus'] =40000;
		return $arr;
	}


}